package com.wt.controller;

import com.wt.exception.MyException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IDEA
 * Describe:
 * Author:tianji
 * Date:2018/10/25
 * Time:19:41
 */
@ControllerAdvice
public class MyExceptionHandler {
    //1.浏览器 客户端返回的都是JSON数据
    /*@ExceptionHandler(MyException.class)
    @ResponseBody
    public Map<String,Object> handlerException(Exception e){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("status","用户不存在");
        map.put("message",e.getMessage());
        return map;
    }*/
    //2.
    @ExceptionHandler(MyException.class)
    public String handlerException(Exception e, HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        //  Integer statusCode =
        // (Integer)request.getAttribute("javax.servlet.error.status_code");
        request.setAttribute("javax.servlet.error.status_code",256);
        map.put("status","用户不存在");
        map.put("message",e.getMessage());
        //转发--->自适应处理
        return "forward:/error";
    }
}
